package top.lenjon.lib.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.lenjon.lib.entity.Book;
import top.lenjon.lib.entity.BookClassify;
import top.lenjon.lib.mapper.BookClassifyMapper;
import top.lenjon.lib.mapper.BookMapper;
import top.lenjon.lib.service.IBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author wei
 * @since 2020-11-09
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookClassifyMapper classifyMapper;

    @Override
    public List<Book> getBooks(Integer pageIndex, Integer pageNum) {
        return bookMapper.getBooks(pageIndex, pageNum);
    }

    @Override
    public List<Book> getBooksByClassify(BookClassify classify) {
        System.out.println(classify);
        /// TODO: 2020/11/10 这个要读取数据好多次。。先放这里，以后手写sql --by wei
//        List<Book> books = new ArrayList<>();
//        classifyMapper.getBookByClassify(classify).forEach(bookId->books.add(this.getById(bookId)));

        return bookMapper.getAllBookByClassify(classify);

    }

    @Override
    public List<Book> getBooksByTitle(String title) {
        return bookMapper.getBooksByTitle(title);
    }

    @Override
    public boolean setBookStatus(int bookId, String status) {
        if("上架".equals(status)){
            return bookMapper.setStatusById(bookId,true)==1;
        }else {
            return bookMapper.setStatusById(bookId,false)==1;
        }
    }


}
